home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_pkgimport.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  3KB  |  99 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. import sys
  6. import string
  7. import random
  8. import tempfile
  9. import unittest
  10. from test.test_support import run_unittest
  11.  
  12. class TestImport(unittest.TestCase):
  13.     
  14.     def __init__(self, *args, **kw):
  15.         self.package_name = 'PACKAGE_'
  16.         while sys.modules.has_key(self.package_name):
  17.             self.package_name += random.choose(string.letters)
  18.             continue
  19.             self
  20.         self.module_name = self.package_name + '.foo'
  21.         unittest.TestCase.__init__(self, *args, **kw)
  22.  
  23.     
  24.     def remove_modules(self):
  25.         for module_name in (self.package_name, self.module_name):
  26.             if sys.modules.has_key(module_name):
  27.                 del sys.modules[module_name]
  28.                 continue
  29.         
  30.  
  31.     
  32.     def setUp(self):
  33.         self.test_dir = tempfile.mkdtemp()
  34.         sys.path.append(self.test_dir)
  35.         self.package_dir = os.path.join(self.test_dir, self.package_name)
  36.         os.mkdir(self.package_dir)
  37.         open(os.path.join(self.package_dir, '__init__' + os.extsep + 'py'), 'w')
  38.         self.module_path = os.path.join(self.package_dir, 'foo' + os.extsep + 'py')
  39.  
  40.     
  41.     def tearDown(self):
  42.         for file in os.listdir(self.package_dir):
  43.             os.remove(os.path.join(self.package_dir, file))
  44.         
  45.         os.rmdir(self.package_dir)
  46.         os.rmdir(self.test_dir)
  47.         self.assertNotEqual(sys.path.count(self.test_dir), 0)
  48.         sys.path.remove(self.test_dir)
  49.         self.remove_modules()
  50.  
  51.     
  52.     def rewrite_file(self, contents):
  53.         for extension in 'co':
  54.             compiled_path = self.module_path + extension
  55.             if os.path.exists(compiled_path):
  56.                 os.remove(compiled_path)
  57.                 continue
  58.         
  59.         f = open(self.module_path, 'w')
  60.         f.write(contents)
  61.         f.close()
  62.  
  63.     
  64.     def test_package_import__semantics(self):
  65.         self.rewrite_file('for')
  66.         
  67.         try:
  68.             __import__(self.module_name)
  69.         except SyntaxError:
  70.             pass
  71.  
  72.         raise RuntimeError, 'Failed to induce SyntaxError'
  73.         if not sys.modules.has_key(self.module_name):
  74.             pass
  75.         self.assert_(not hasattr(sys.modules[self.package_name], 'foo'))
  76.         var = 'a'
  77.         while var in dir(__builtins__):
  78.             var += random.choose(string.letters)
  79.         self.rewrite_file(var)
  80.         
  81.         try:
  82.             __import__(self.module_name)
  83.         except NameError:
  84.             pass
  85.  
  86.         raise RuntimeError, 'Failed to induce NameError.'
  87.         self.rewrite_file('%s = 1' % var)
  88.         module = __import__(self.module_name).foo
  89.         self.assertEqual(getattr(module, var), 1)
  90.  
  91.  
  92.  
  93. def test_main():
  94.     run_unittest(TestImport)
  95.  
  96. if __name__ == '__main__':
  97.     test_main()
  98.  
  99.